////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   SimplePageEditStyleActionTemplate.java
// AUTHOR           :   ric.yik
// DATE CREATE      :   2006/5/5
// DESCRIPTION      :   
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/5/5      ric.yik         1.0                 source is created.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.framework.controller.action;

import java.util.HashMap;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.util.web.WebUtil;
import com.erp.common.web.ActionMethodDispatcher;
import com.erp.common.web.PrefixActionMethodDispatcher;
import com.erp.framework.controller.form.SimplePageEditStyleForm;
import com.erp.framework.model.schema.MetaData;
import com.erp.framework.model.schema.TableSchema;
import com.erp.framework.util.BeanUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

public abstract class SimplePageEditStyleActionTemplate extends SimplePageEditStyleAction {
	
	protected void afterExecuteAction(ActionMapping mapping, ActionForm formIn, HttpServletRequest request, HttpServletResponse response) {
		  HttpSession session = request.getSession(false);
		  
		  String companyCode = WebUtil.getAppropriateCompanyCode(session, null);
		  BeanUtil.setPropertyDefault(formIn, "srchCompanyCode", companyCode);		  
		  super.afterExecuteAction(mapping, formIn, request, response);
	}

    protected ActionForward handlePageEvent(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {
        ActionForward actionForward = new ActionForward(mapping.getInput());
        if (this.isHandled(request)) {
            return actionForward;
        }
        try {
            SimplePageEditStyleForm form = (SimplePageEditStyleForm) formIn;
            actionForward = new ActionMethodDispatcher()
                .setAction(this)
                .setDefaultActionForward(actionForward)
                .setNextDispatcher(new PrefixActionMethodDispatcher().addPrefix(ProjectConstants.ACTION_SPEDIT))
                .processAction(form.getPageEvent(), mapping, form, request, response, errors);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
        this.saveToken(request);
        this.setHandled(request);
        return actionForward;
    }

    protected ActionForward processOtherStatus(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors, String rb_status) {
        ActionForward actionForward = new ActionForward(mapping.getInput());
        try {
            SimplePageEditStyleForm form = (SimplePageEditStyleForm) formIn;
            actionForward = new ActionMethodDispatcher()
                .setAction(this)
                .setDefaultActionForward(actionForward)
                .setNextDispatcher(new PrefixActionMethodDispatcher().addPrefix(ProjectConstants.STATUS_SPSET))
                .setProcessStatus(true)
                .setStatus(rb_status)
                .processAction(form.getPageEvent(), mapping, form, request, response, errors);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
        this.saveToken(request);
        this.setHandled(request);
        return actionForward;
    }

    public ActionForward processActionSpEdit(ActionMapping mapping, ActionForm formIn, 
            HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
        SimplePageEditStyleForm form = (SimplePageEditStyleForm) formIn;
        HttpSession session = request.getSession(false);
        ActionForward actionForward = new ActionForward(mapping.getInput());
        String action = CommonUtil.nullToString(form.getPageEvent());
        session.removeAttribute(ProjectConstants.SESSION_SELECT_REMARK_TEXT_PARAMETERS);
        String fieldName = BeanUtil.toPropertyName(form.getPageEvent().substring(ProjectConstants.ACTION_SPEDIT.length()));
        String fieldValue = "";
        String eventSrc = form.getPageEventSrc();
        LinkedList keyList = new LinkedList();
        boolean readonly = false;
        String fieldObjectId = "";
        Object formOrMultiRow = form;
        TableSchema ts = form.getTs();
        if (CommonUtil.isValidInteger(eventSrc)) {
            formOrMultiRow = WebUtil.getCurrentRow(form);
        }
        MetaData metadata = ts.getMetaDataByVoName(fieldName);
        fieldObjectId = metadata.getWebObjectId();
        String columnName = metadata.getWebLabelName();
        fieldValue = String.valueOf(BeanUtil.getPropertyDefault(formOrMultiRow, fieldName));
        HashMap params = new HashMap();
        params.put(ProjectConstants.SESSION_SESSION_VALUE, this.getSessionSimpleMastersStatus());
        params.put(ProjectConstants.SESSION_OPENER_PATH, getFunctionId());
        params.put(ProjectConstants.SESSION_KEY_LIST, keyList);
        if (readonly) params.put(ProjectConstants.SESSION_SIMPLE_READ_ONLY, ProjectConstants.GENERIC_YES);
        params.put(ProjectConstants.SESSION_FIELD, fieldName);
        if (!CommonUtil.isEmpty(eventSrc)) params.put(ProjectConstants.SESSION_EVENTSRC, eventSrc);
        params.put(ProjectConstants.SESSION_EDIT_COLUMN_NAME, columnName);
        params.put(ProjectConstants.SESSION_EDIT_VALUE, fieldValue);
        params.put(ProjectConstants.SESSION_EDIT_COLUMN_OBJECT_ID, fieldObjectId);
        this.setValuesForActionSpEdit(mapping, formIn, request, response, errors, formOrMultiRow, ts, keyList, params);
        session.setAttribute(ProjectConstants.SESSION_SELECT_REMARK_TEXT_PARAMETERS, params);
        return actionForward;
    }

    protected void setValuesForActionSpEdit(ActionMapping mapping, ActionForm formIn, 
            HttpServletRequest request, HttpServletResponse response, ActionErrors errors, Object formOrMultiRow, TableSchema ts, LinkedList keyList, HashMap params) {
    }

    public ActionForward processStatusSPSet(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors, String rb_status) {
        SimplePageEditStyleForm form = (SimplePageEditStyleForm) formIn;
        HttpSession session = request.getSession(false);
        ActionForward actionForward = new ActionForward(mapping.getInput());
        HashMap params = (HashMap) session.getAttribute(ProjectConstants.SESSION_SELECT_REMARK_TEXT_PARAMETERS);
        session.removeAttribute(ProjectConstants.SESSION_SELECT_REMARK_TEXT_PARAMETERS);
        String editValue = (String) params.get(ProjectConstants.SESSION_EDIT_VALUE);
        String columnName = (String) params.get(ProjectConstants.SESSION_EDIT_COLUMN_NAME);
        String fieldObjectId = (String) params.get(ProjectConstants.SESSION_EDIT_COLUMN_OBJECT_ID);
        String fieldName = (String) params.get(ProjectConstants.SESSION_FIELD);
        String eventSrc = (String) params.get(ProjectConstants.SESSION_EVENTSRC);
        Object formOrMultiRow = form;
        TableSchema ts = form.getTs();
        if (CommonUtil.isValidInteger(eventSrc)) {
            formOrMultiRow = WebUtil.getCurrentRow(form);
        }
        BeanUtil.setPropertyDefault(formOrMultiRow, fieldName, editValue);
        return actionForward;
    }

}
